package com.shoes.controller;

import com.alibaba.fastjson.JSON;
import com.shoes.model.ResponseDO;
import com.shoes.service.GoodsService;
import com.shoes.utils.LoggerUtil;
import com.shoes.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author asen
 */
@Controller
@RequestMapping(value = "/goods", produces = {"application/json;charset=UTF-8"})
public class GoodsController {

    private GoodsService goodsService;

    @RequestMapping(value = "/info", method = RequestMethod.GET)
    @ResponseBody
    public String getGoodsInfo(HttpServletRequest request) throws ServletException, IOException {
        ResponseDO result;

        try {
            long userId = Long.parseLong(request.getParameter("user_id"));
            long goodsId = Long.parseLong(request.getParameter("goods_id"));

            result = goodsService.getGoodsInfoByGoodsId(userId, goodsId);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            result = new ResponseDO(ResultCode.PARAMETER_ERROR, null);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResponseDO(ResultCode.GLOBAL_FAILED, null);
        }

        String data = JSON.toJSONString(result);
        LoggerUtil.logger.info(" Response ResultCode: " + result.getResultCode());
        LoggerUtil.logger.info(" Response Content:\n" + data);
        return data;
    }

    @RequestMapping(value = "/sellingList", method = RequestMethod.GET)
    @ResponseBody
    public String getSellingGoodsList(HttpServletRequest request) throws ServletException, IOException {
        ResponseDO result;

        try {
            long storeId = Long.parseLong(request.getParameter("store_id"));

            result = goodsService.getSellingGoodsList(storeId);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            result = new ResponseDO(ResultCode.PARAMETER_ERROR, null);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResponseDO(ResultCode.GLOBAL_FAILED, null);
        }

        String data = JSON.toJSONString(result);
        LoggerUtil.logger.info(" Response ResultCode: " + result.getResultCode());
        LoggerUtil.logger.info(" Response Content:\n" + data);
        return data;
    }

    @RequestMapping(value = "/repertoryList", method = RequestMethod.GET)
    @ResponseBody
    public String getRepertoryGoodsList(HttpServletRequest request) throws ServletException, IOException {
        ResponseDO result;

        try {
            long storeId = Long.parseLong(request.getParameter("store_id"));

            result = goodsService.getRepertoryGoodsList(storeId);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            result = new ResponseDO(ResultCode.PARAMETER_ERROR, null);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResponseDO(ResultCode.GLOBAL_FAILED, null);
        }

        String data = JSON.toJSONString(result);
        LoggerUtil.logger.info(" Response ResultCode: " + result.getResultCode());
        LoggerUtil.logger.info(" Response Content:\n" + data);
        return data;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public String addGoods(HttpServletRequest request) throws ServletException, IOException {
        ResponseDO result;

        try {
            long storeId = Long.parseLong(request.getParameter("store_id"));
            String name = request.getParameter("name");
            double price = Double.parseDouble(request.getParameter("price"));
            String categoryIds = request.getParameter("category_ids");
            int isLike = Integer.parseInt(request.getParameter("is_like"));

            result = goodsService.addGoods(storeId, name, price, categoryIds, isLike);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            result = new ResponseDO(ResultCode.PARAMETER_ERROR, null);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResponseDO(ResultCode.GLOBAL_FAILED, null);
        }

        String data = JSON.toJSONString(result);
        LoggerUtil.logger.info(" Response ResultCode: " + result.getResultCode());
        LoggerUtil.logger.info(" Response Content:\n" + data);
        return data;
    }

    public GoodsService getGoodsService() {
        return goodsService;
    }
    @Autowired
    public void setGoodsService(GoodsService goodsService) {
        this.goodsService = goodsService;
    }
}
